const {query}= require('../util/query')
/*
分组是否存在
 */
let findGroup=async function(name){
  let _sql=`SELECT * FROM ALL_GROUP where GROUP_NAME ="${name}" limit 1;`
  let result=await query(_sql)
  if (Array.isArray(result) && result.length > 0) {
    result = result[0]
  } else {
    result = null
  }
  return result
}
/*
创建分组
 */
let createGroup=async function(value,groupInfo){
  let _sql =
    `INSERT INTO ALL_GROUP set ?;`
  let result=await query(_sql,value)
  let result_group=await inviteToGroup(groupInfo)
  return Promise.all([result,result_group])
}
/*
校检是否为组织创建人,返回true和false
 */
let isLeader=async (userId,groupId)=>{
  let _sql=`SELECT * FROM USER_GROUP WHERE GROUP_ID ='${groupId}' AND USER_ID = '${userId}' AND AUTH = '1001' limit 1`
  let result=await query(_sql)
  result = Array.isArray(result) && result.length > 0;
  return result
}
/*
用户是否在组织中
 */
let isInGroup=async (userId,groupId)=>{
  let _sql=`SELECT * FROM USER_GROUP WHERE GROUP_ID ='${groupId}' AND USER_ID = '${userId}'limit 1`
  let result=await query(_sql)
  result = Array.isArray(result) && result.length > 0;
  return result
}
/*
组织交接
 */
let transLeader=async function(groupId,userId){

}
/*
邀请人进分组
 */
let inviteToGroup=async function(info){
  let _sql=`INSERT INTO USER_GROUP set ?;`
  return query(_sql,info)
}
/*
查看此用户所在的所有分组
 */
let getGroupList = async function(id){
  let _sql = `
      SELECT * FROM ALL_GROUP  where ID= ANY (SELECT GROUP_ID FROM USER_GROUP WHERE USER_ID='${id}') ;
  `
  return await query(_sql)
}
/*
查看该分组所有的用户
 */
let getAllGroupUser = async function(id){
  let _sql = `
      SELECT u.*,r.* FROM USER u,USER_GROUP r where u.ID= ANY (SELECT USER_ID FROM USER_GROUP WHERE GROUP_ID='${id}') and r.USER_ID=u.ID and r.GROUP_ID='${id}';
  `
  return await query(_sql)
}
/*
给用户分配组权限
 */
let allocateGroupAuth = async function(id,groupId,newAuth){
  let _sql = `
      UPDATE USER_GROUP SET AUTH = '${newAuth}' WHERE USER_ID='${id}' and GROUP_ID='${groupId}';
  `
  return await query(_sql)
}
/*
将用户踢出团队
 */
let deleteFromGroup = async function(id,groupId){
  let _sql = `
      DELETE FROM USER_GROUP WHERE USER_ID ='${id}' AND GROUP_ID ='${groupId}'
  `
  return await query(_sql)
}
/*
更新团队信息
 */
let updateGroup = async function(groupId,name,des){
  let _sql = `
      UPDATE ALL_GROUP SET GROUP_NAME='${name}',DESCRIPTION='${des}' WHERE ID='${groupId}'
  `
  return await query(_sql)
}
/*
解散团队
 */
let dissolutionGroup = async function(groupId){
  let sql_group = `
      DELETE FROM ALL_GROUP WHERE ID ='${groupId}';
  `
  let sql_groupUser = `
      DELETE FROM USER_GROUP WHERE GROUP_ID ='${groupId}';
  `
  return Promise.all([await query(sql_groupUser),await query(sql_group)])
}
/*
根据团队ID查团队名
 */
let getNameById = async function(groupId){
  let sql = `
      SELECT GROUP_NAME FROM ALL_GROUP WHERE ID ='${groupId}' limit 1;
  `
  let result = await query(sql)
  return result[0]['GROUP_NAME']
}
/*
查询当前用户在当前组织中的权限
 */
let getAuth = async function(groupId,userId){
  let sql = `
      SELECT AUTH FROM USER_GROUP WHERE GROUP_ID ='${groupId}'and USER_ID='${userId}' limit 1;
  `
  let result = await query(sql)
  return result[0]['AUTH']
}

module.exports = {
  findGroup,
  createGroup,
  isLeader,
  transLeader,
  inviteToGroup,
  getGroupList,
  isInGroup,
  getAllGroupUser,
  allocateGroupAuth,
  deleteFromGroup,
  updateGroup,
  dissolutionGroup,
  getNameById,
  getAuth
}
